//
//  ChangeHealthPasswordViewController.m
//  1gene
//
//  Created by 1jiyin on 16/4/7.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "ChangeHealthPasswordViewController.h"

@interface ChangeHealthPasswordViewController ()
@property(nonatomic,copy)UITextField *oldPasswordTextField;
@property(nonatomic,copy)UITextField *myNewPasswordTextField;
@property(nonatomic,copy)UITextField *sureNewPasswordTextField;
@end

@implementation ChangeHealthPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:[UIView new]];
    [self createNavigation];
    [self createUI];
    NSLog(@"开始修改健康档案密码");
    // Do any additional setup after loading the view.
}
-(void)createUI
{
    UIView *phoneView1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 54, 40)];
    //phoneView.backgroundColor=[UIColor colorWithRed:146/255.0 green:208/255.0 blue:234/255.0 alpha:1];
    
    UIView *backgroundView1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, phoneView1.frame.size.width-5, phoneView1.frame.size.height)];
    backgroundView1.backgroundColor=[UIColor colorWithRed:146/255.0 green:208/255.0 blue:234/255.0 alpha:1];
    [phoneView1 addSubview:backgroundView1];
    
    
    
    UIImageView *phoneImgView1=[[UIImageView alloc]initWithFrame:CGRectMake(10, 4, 30, 40-2*2-4)];
    UIImage *phoneImg1=[UIImage imageNamed:@"password-3-2"];
    phoneImg1=[phoneImg1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    phoneImgView1.image=phoneImg1;
    [phoneView1 addSubview:phoneImgView1];
    
    
    
    
    UIView *phoneView2=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 54, 40)];
    //phoneView.backgroundColor=[UIColor colorWithRed:146/255.0 green:208/255.0 blue:234/255.0 alpha:1];
    
    UIView *backgroundView2=[[UIView alloc]initWithFrame:CGRectMake(0, 0, phoneView2.frame.size.width-5, phoneView2.frame.size.height)];
    backgroundView2.backgroundColor=[UIColor colorWithRed:146/255.0 green:208/255.0 blue:234/255.0 alpha:1];
    [phoneView2 addSubview:backgroundView2];
    
    UIImageView *phoneImgView2=[[UIImageView alloc]initWithFrame:CGRectMake(10, 4, 30, 40-2*2-4)];
    UIImage *phoneImg2=[UIImage imageNamed:@"password-3-2"];
    phoneImg2=[phoneImg2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    phoneImgView2.image=phoneImg2;
    [phoneView2 addSubview:phoneImgView2];

    
    
    UIView *phoneView3=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 54, 40)];
    //phoneView.backgroundColor=[UIColor colorWithRed:146/255.0 green:208/255.0 blue:234/255.0 alpha:1];
    
    UIView *backgroundView3=[[UIView alloc]initWithFrame:CGRectMake(0, 0, phoneView3.frame.size.width-5, phoneView3.frame.size.height)];
    backgroundView3.backgroundColor=[UIColor colorWithRed:146/255.0 green:208/255.0 blue:234/255.0 alpha:1];
    [phoneView3 addSubview:backgroundView3];
    
    UIImageView *phoneImgView3=[[UIImageView alloc]initWithFrame:CGRectMake(10, 4, 30, 40-2*2-4)];
    UIImage *phoneImg3=[UIImage imageNamed:@"password-3-2"];
    phoneImg3=[phoneImg3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    phoneImgView3.image=phoneImg3;
    [phoneView3 addSubview:phoneImgView3];
    
    
    
    
    
    
    
    
    //原始密码
    UITextField *oldPasswordTextField=[[UITextField alloc]initWithFrame:CGRectMake(10, 20+44+20, self.view.frame.size.width-10*2, 40)];
    oldPasswordTextField.leftViewMode=UITextFieldViewModeAlways;
    UIImageView *oldPasswordLeftView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 30, 30)];
    oldPasswordLeftView.image=[UIImage imageNamed:@"password-3-2.png"];
    oldPasswordTextField.leftView=oldPasswordLeftView;
    oldPasswordTextField.placeholder=@"原始密码";
    oldPasswordTextField.font=[UIFont systemFontOfSize:15];
    oldPasswordTextField.textColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
    oldPasswordTextField.backgroundColor=[UIColor whiteColor];
    oldPasswordTextField.layer.borderColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1].CGColor;
    oldPasswordTextField.layer.borderWidth=1.0;
    oldPasswordTextField.layer.cornerRadius=5.0;
    [self.view addSubview:oldPasswordTextField];
    oldPasswordTextField.leftView=phoneView1;
    oldPasswordTextField.leftViewMode=UITextFieldViewModeAlways;
    
    
    //新密码
    UITextField *newPasswordTextField=[[UITextField alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(oldPasswordTextField.frame)+10, self.view.frame.size.width-10*2, 40)];
    newPasswordTextField.leftViewMode=UITextFieldViewModeAlways;
    UIImageView *newPasswordLeftView=[[UIImageView alloc]initWithFrame:CGRectMake(15, 5, 30, 30)];
    newPasswordLeftView.image=[UIImage imageNamed:@"password-3-2.png"];
    newPasswordTextField.leftView=newPasswordLeftView;
    newPasswordTextField.placeholder=@"新密码";
    newPasswordTextField.font=[UIFont systemFontOfSize:15];
    newPasswordTextField.textColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
    newPasswordTextField.backgroundColor=[UIColor whiteColor];
    newPasswordTextField.layer.borderColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1].CGColor;
    newPasswordTextField.layer.borderWidth=1.0;
    newPasswordTextField.layer.cornerRadius=5.0;
    [self.view addSubview:newPasswordTextField];
    newPasswordTextField.leftView=phoneView2;
    newPasswordTextField.leftViewMode=UITextFieldViewModeAlways;
    
    //确认新密码
    UITextField *sureNewPasswordTextField=[[UITextField alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(newPasswordTextField.frame)+10, self.view.frame.size.width-10*2, 40)];
    sureNewPasswordTextField.leftViewMode=UITextFieldViewModeAlways;
    UIImageView *sureNewPasswordLeftView=[[UIImageView alloc]initWithFrame:CGRectMake(15, 5, 30, 30)];
    sureNewPasswordLeftView.image=[UIImage imageNamed:@"password-3-2.png"];
    sureNewPasswordTextField.leftView=sureNewPasswordLeftView;
    sureNewPasswordTextField.placeholder=@"确认新密码";
    sureNewPasswordTextField.font=[UIFont systemFontOfSize:15];
    sureNewPasswordTextField.textColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
    sureNewPasswordTextField.backgroundColor=[UIColor whiteColor];
    sureNewPasswordTextField.layer.borderColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1].CGColor;
    sureNewPasswordTextField.layer.borderWidth=1.0;
    sureNewPasswordTextField.layer.cornerRadius=5.0;
    [self.view addSubview:sureNewPasswordTextField];
    sureNewPasswordTextField.leftView=phoneView3;
    sureNewPasswordTextField.leftViewMode=UITextFieldViewModeAlways;
    
    
    
    //确认修改
    UIButton *changeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    changeBtn.frame=CGRectMake((self.view.frame.size.width-150)/2.0, CGRectGetMaxY(sureNewPasswordTextField.frame)+20, 150, 35);
    [changeBtn setTitle:@"确认" forState:UIControlStateNormal];
    [changeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    changeBtn.titleLabel.font=[UIFont systemFontOfSize:20];
    changeBtn.backgroundColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1];
    changeBtn.layer.borderColor=[UIColor colorWithRed:87/255.0 green:187/255.0 blue:215/255.0 alpha:1].CGColor;
    changeBtn.layer.borderWidth=2.0;
    changeBtn.layer.cornerRadius=5.0;
    [changeBtn addTarget:self action:@selector(changeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeBtn];
    _oldPasswordTextField=oldPasswordTextField;
    _myNewPasswordTextField=newPasswordTextField;
    _sureNewPasswordTextField=sureNewPasswordTextField;
    
}
//确认修改
-(void)changeBtnClick:(UIButton *)sender
{
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    BOOL setHealthPassword=[user objectForKey:@"sethealthPassword"];
    NSString *oldPassword=[user objectForKey:@"healthPassword"];
    if (setHealthPassword) {
        if (_oldPasswordTextField.text.length>0&&_myNewPasswordTextField.text.length>0&&_sureNewPasswordTextField.text.length>0) {
            if ([_oldPasswordTextField.text isEqualToString:oldPassword]) {
                if ([_myNewPasswordTextField.text isEqualToString:_sureNewPasswordTextField.text]) {
                    if ([_myNewPasswordTextField.text isEqualToString:oldPassword]) {
                        [self.view showTiShiMessage:@"新旧密码不能一样"];
                    }else{
                        [self changeHealthPassword];
                    }
                }else{
                    [self.view showTiShiMessage:@"输入的密码不一致"];
                }

            }else{
                [self.view showTiShiMessage:@"密码错误"];
            }
        }else{
            [self.view showTiShiMessage:@"密码不能为空"];
        }
        
    }
}

-(void)createNavigation
{
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"修改密码";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//修改
-(void)changeHealthPassword
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    NSDictionary *params=@{@"privacy_pwd":_oldPasswordTextField.text,@"newprivacy_pwd":_myNewPasswordTextField.text,@"newprivacy_pwd_comfired":_sureNewPasswordTextField.text};
    
    [self.view isLoading:YES];
    [manager POST:[NSString stringWithFormat:SetHealthPassword,token] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.view isLoading:NO];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"修改健康档案密码%@",dic);
        NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
        [user setBool:YES forKey:@"sethealthPassword"];
        [user setObject:_myNewPasswordTextField.text forKey:@"healthPassword"];
        [user synchronize];
        [self.view showTiShiMessage:@"修改成功"];
        NSLog(@"=====%@",dic);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view isLoading:NO];
        NSLog(@"error%@",error);
        [self.view showTiShiMessage:@"修改失败"];
    }];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
